import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { equipmentStateType } from './typings'
import {
  getNodeByParentCode,
  selectBoxListByKey,
  deviceListGet,
  deviceAdd,
  deviceUpdate,
  getTenantBrandList,
  getTenantModelList,
  selectBoxListByName,
  deviceRemove,
  deviceView,
  deviceExport,
  viewByDeviceIdGet,
  updateConnectionPut,
  updateWorkPut,
  updateLoopPut,
  updateAlarmPut,
  updateEventPut,
  updateOtherPut,
  getIssueCommandGet,
  getQueryCommandGet
} from '~/api/monitor/equipment'
import { getDataDictionaryByCode } from '~/api/global'
import { filterValueEnum, DownloadAttachment } from '~/utils/util'

const equipment: Module<equipmentStateType, equipmentStateType> = {
  namespaced: true,
  state: {
    //  运行状态
    fnStateOpt: [],
    fnStateEnum: {},
    //  通信状态
    communicationStateOpt: [],
    communicationStateEnum: {},
    //  所属设施
    facilitiesStateOpt: [],
    //  设备分类
    classificationStateOpt: [],
    //  设备品牌
    equipmentBrandOpt: [],
    //  设备型号
    equipmentModelOpt: [],
    //  配电设施
    powerFacilitiesOpt: []
  },
  getters: {
    fnStateOpt: (state) => state.fnStateOpt,
    fnStateEnum: (state) => state.fnStateEnum,
    communicationStateOpt: (state) => state.communicationStateOpt,
    communicationStateEnum: (state) => state.communicationStateEnum,
    facilitiesStateOpt: (state) => state.facilitiesStateOpt,
    classificationStateOpt: (state) => state.classificationStateOpt,
    equipmentBrandOpt: (state) => state.equipmentBrandOpt,
    equipmentModelOpt: (state) => state.equipmentModelOpt,
    powerFacilitiesOpt: (state) => state.powerFacilitiesOpt
  },
  mutations: {
    SET_NODEBYPARENTCODE(state, payload) {
      state.classificationStateOpt = payload
    },
    SET_SELECTBOXLISTBYKEY(state, payload) {
      state.facilitiesStateOpt = payload
    },
    SET_FNSTATE(state, payload) {
      state.fnStateOpt = payload
      state.fnStateEnum = filterValueEnum(payload, 'dictKey', 'dictValue')
    },
    SET_COMMUNICATION(state, payload) {
      state.communicationStateOpt = payload
      state.communicationStateEnum = filterValueEnum(payload, 'dictKey', 'dictValue')
    },
    SET_BRANDLIST(state, payload) {
      state.equipmentBrandOpt = payload
    },
    SET_MODELLIST(state, payload) {
      state.equipmentModelOpt = payload
    },
    SET_SELECTBOXLIST(state, payload) {
      state.powerFacilitiesOpt = payload
    }
  },
  actions: {
    //  设备管理-编辑
    DeviceUpdate<S, T extends { id?: string }>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        deviceUpdate<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  设备管理-新增
    DeviceAdd<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        deviceAdd<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  设备管理-列表
    DeviceListGet<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        deviceListGet<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  设备管理-删除
    DeviceRemove<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        deviceRemove<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  设备管理-详情
    DeviceView<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        deviceView<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  运行状态
    FnStateGet<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getDataDictionaryByCode<T>(params)
          .then((res: AxiosResponse) => {
            commit('SET_FNSTATE', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    //  通信状态
    CommunicationStateGet<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getDataDictionaryByCode<T>(params)
          .then((res: AxiosResponse) => {
            commit('SET_COMMUNICATION', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 设备列表-导出
    async DeviceExport<S, T>(_: ActionContext<S, T>, params: T) {
      return await deviceExport<T>(params).then((res: AxiosResponse) => {
        const contentDisposition = res?.headers?.['content-disposition']
        if (contentDisposition && res.data) {
          DownloadAttachment(decodeURI(contentDisposition.substring(20)), res.data)
        }
        return res.data
      })
    },
    // 设备品牌
    async GetTenantBrandList<S, T>({ commit }: ActionContext<S, T>) {
      return await getTenantBrandList({}).then((res: AxiosResponse) => {
        commit('SET_BRANDLIST', res?.data?.data || [])
        return res?.data
      })
    },
    // 设备型号
    async GetTenantModelList<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return await getTenantModelList(params).then((res: AxiosResponse) => {
        commit('SET_MODELLIST', res?.data?.data || [])
        return res?.data
      })
    },
    // 配电设施
    async SelectBoxListByName<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return await selectBoxListByName(params).then((res: AxiosResponse) => {
        commit('SET_SELECTBOXLIST', res?.data?.data || [])
        return res?.data
      })
    },
    // 设备分类
    async GetNodeByParentCode<S, T>({ commit }: ActionContext<S, T>) {
      return await getNodeByParentCode({}).then((res: AxiosResponse) => {
        commit('SET_NODEBYPARENTCODE', res?.data?.data || [])
        return res?.data
      })
    },
    // 所属设施
    async SelectBoxListByKey<S, T>({ commit }: ActionContext<S, T>) {
      return await selectBoxListByKey({}).then((res: AxiosResponse) => {
        commit('SET_SELECTBOXLISTBYKEY', res?.data?.data || [])
        return res?.data
      })
    },
    // 设备管理-参数详情
    ViewByDeviceIdGet<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        viewByDeviceIdGet<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 设备管理-参数详情
    UpdateConnectionPut<S, T extends { id?: string }>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        updateConnectionPut<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 设备管理-工作参数修改
    UpdateWorkPut<S, T extends { id?: string }>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        updateWorkPut<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 设备管理-回路参数修改
    UpdateLoopPut<S, T extends { id?: string; list: any }>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        updateLoopPut<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 设备管理-警报参数修改
    UpdateAlarmPut<S, T extends { id?: string }>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        updateAlarmPut<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 设备管理-事件参数修改
    UpdateEventPut<S, T extends { id?: string; list: any }>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        updateEventPut<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 设备管理-其他参数修改
    UpdateOtherPut<S, T extends { id?: string }>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        updateOtherPut<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 设备管理-查询监控箱下发指令
    GetIssueCommandGet<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getIssueCommandGet<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 设备管理-查询监控箱查询指令
    GetQueryCommandGet<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getQueryCommandGet<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    }
  }
}

export default equipment
